package org.xu.logic;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Duration;

/**
 * 本地缓存 - 测试
 * @author liuxiaoxu
 */
@Slf4j
@Service
public class LocalCacheLogicImpl implements LocalCacheLogic{

    private final LoadingCache<String, String> tokenCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(5)).build(key -> getToken(key));

    /**
     * 获取token
     */
    private String getToken(String account) {
        log.info("重新获取cache {}",account);
        return account+ "_token";
    }

    @Override
    public String getCacheToken(String account) {
        return tokenCache.get(account);
    }
}
